<!--
 * @Descripttion: (子篮信息/cks_basket)
 * @Author: (admin)
 * @Date: (2025-02-23)
-->
<template>
    <div>
      <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
       
        <el-form-item :label="$t('basketTable.col1')" prop="blueCode">
            <el-input v-model="queryParams.blueCode" placeholder="" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item :label="$t('basketTable.col2')" prop="Type">
            <el-select v-model="queryParams.Type" placeholder="" clearable style="width: 240px">
              <el-option :label="$t('part_type.type00')" :value="-1" />
              <el-option v-for="dict in options.typeOptions" :key="dict.dictValue"
              :label="$t(dict.langKey)" :value="dict.dictValue" />
            </el-select>
          </el-form-item>
        <el-form-item>
          <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
          <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
        </el-form-item>
      </el-form>
      <!-- 工具区域 -->
      <el-row :gutter="15" class="mb10">
        <!-- <el-col :span="1.5">
            <el-button type="info" plain icon="Upload" @click="handleImport" v-hasPermi="['system:user:import']">
              {{ $t('btn.import') }}
            </el-button>
          </el-col> -->
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
      </el-row>
  
      <el-table
        :data="dataList"
        v-loading="loading"
        ref="table"
        border
        header-cell-class-name="el-table-header-cell"
        highlight-current-row
        @sort-change="sortChange"
        >
        <!-- <el-table-column prop="id" label="Id" align="center" v-if="columns.showColumn('id')"/> -->
        <el-table-column prop="blueCode" :label="$t('basketTable.col1')" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('blueCode')"/>
        <el-table-column prop="type" :label="$t('basketTable.col2')" align="center" v-if="columns.showColumn('type')">
          <template #default="scope">
            <dict-tag :options=" options.typeOptions" :value="scope.row.type"  />
          </template>
        </el-table-column>
        <el-table-column prop="status" :label="$t('basketTable.col3')" align="center" v-if="columns.showColumn('status')">
            <template #default="scope">
                <dict-tag :options=" options.statusOptions" :value="scope.row.status"  />
            </template>
        </el-table-column>
        
        <el-table-column prop="stock" :label="$t('basketTable.col4')" align="center" v-if="columns.showColumn('stock')"/>
        
        <el-table-column prop="updateTime" :label="$t('basketTable.col5')" :show-overflow-tooltip="true"  v-if="columns.showColumn('updateTime')"/>
        <!-- <el-table-column prop="createdTime" label="创建时间" :show-overflow-tooltip="true"  v-if="columns.showColumn('createdTime')"/>
        <el-table-column prop="isDeleted" label="IsDeleted" align="center" v-if="columns.showColumn('isDeleted')">
          <template #default="scope">
            <dict-tag :options=" options.isDeletedOptions" :value="scope.row.isDeleted"  />
          </template>
        </el-table-column> -->
        <el-table-column :label="$t('basketTable.col6')" width="100">
          <template #default="scope">
            <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['cksbasket:edit']" @click="handleUpdate(scope.row)"></el-button>
            <!-- <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['cksbasket:delete']" @click="handleDelete(scope.row)"></el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
  
  
      <!-- 添加或修改子篮信息对话框 -->
      <el-dialog :title="title" :lock-scroll="false" v-model="open" >
        <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
          <el-row :gutter="20">
              
            <el-col :lg="12" v-if="opertype != 1">
              <el-form-item label="Id" prop="id">
                <el-input-number v-model.number="form.id" controls-position="right" placeholder="请输入Id" :disabled="true"/>
              </el-form-item>
            </el-col>
  
            <el-col :lg="12">
              <el-form-item label="子篮码" prop="blueCode">
                <el-input v-model="form.blueCode" placeholder="请输入子篮码" :disabled="true" />
              </el-form-item>
            </el-col>
              
            <el-col :lg="12">
              <el-form-item label="子篮类型" prop="type">
                <el-select v-model="form.type"  placeholder="请选择子篮类型" :disabled="true">
                  <el-option
                    v-for="item in options.typeOptions" 
                    :key="item.dictValue" 
                    :label="item.dictLabel" 
                    :value="parseInt(item.dictValue)"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
              
            <el-col :lg="12">
              <el-form-item label="零件数量" prop="stock">
                <el-input v-model.number="form.stock" placeholder="请输入零件库存" />
              </el-form-item>
            </el-col>
  
            <!-- <el-col :lg="12">
              <el-form-item label="最后更新时间" prop="updateTime">
                <el-date-picker v-model="form.updateTime" type="datetime" :teleported="false" placeholder="选择日期时间"></el-date-picker>
              </el-form-item>
            </el-col> -->
  
            
          </el-row>
        </el-form>
        <template #footer v-if="opertype != 3">
          <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
          <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
        </template>
      </el-dialog>

      <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" v-model="upload.open" width="400px" append-to-body>
      <el-upload
        name="file"
        ref="uploadRef"
        :limit="1"
        accept=".xlsx,.xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag>
        <el-icon class="el-icon--upload">
          <upload-filled />
        </el-icon>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <template #tip>
          <div class="el-upload__tip text-center">
            <!-- <div class="el-upload__tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div> -->
            <span>仅允许导入xls、xlsx格式文件。</span>
            <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>
          </div>
        </template>
      </el-upload>
      <template #footer>
        <el-button @click="upload.open = false">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitFileForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>

    </div>
  </template>
  
  <script setup name="cksbasket">
  import { listCksBasket,
   addCksBasket, delCksBasket, 
   updateCksBasket,getCksBasket, 
   } 
  from '@/api/business/cksbasket.js'
  import { getToken } from '@/utils/auth'
  const { proxy } = getCurrentInstance()
  const ids = ref([])
  const loading = ref(false)
  const showSearch = ref(true)
  const queryParams = reactive({
    pageNum: 1,
    pageSize: 10,
    sort: '',
    sortType: 'asc',
    
  })
  const columns = ref([
    //{ visible: false, prop: 'id', label: 'Id' },
    { visible: true, prop: 'blueCode', label: '子篮码' },
    { visible: true, prop: 'type', label: '子篮类型' },
    { visible: true, prop: 'stock', label: '零件库存' },
    { visible: true, prop: 'updateTime', label: '最后更新时间' },
    //{ visible: false, prop: 'createdTime', label: '创建时间' },
    //{ visible: false, prop: 'isDeleted', label: 'IsDeleted' },
  ])
  const total = ref(0)
  const dataList = ref([])
  const queryRef = ref()
  const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])
  
  
  var dictParams = [
  ]
  
  
  function getList(){
    loading.value = true
    listCksBasket(queryParams).then(res => {
      const { code, data } = res
      if (code == 200) {
        dataList.value = data.result
        total.value = data.totalNum
        loading.value = false
      }
    })
  }
  
  // 查询
  function handleQuery() {
    queryParams.pageNum = 1
    getList()
  }
  
  // 重置查询操作
  function resetQuery(){
    proxy.resetForm("queryRef")
    handleQuery()
  }
  // 自定义排序
  function sortChange(column) {
    var sort = undefined
    var sortType = undefined
  
    if (column.prop != null && column.order != null) {
      sort = column.prop
      sortType = column.order
  
    }
    queryParams.sort = sort
    queryParams.sortType = sortType
    handleQuery()
  }
  
  /*************** form操作 ***************/
  const formRef = ref()
  const title = ref('')
  // 操作类型 1、add 2、edit 3、view
  const opertype = ref(0)
  const open = ref(false)
  const state = reactive({
    single: true,
    multiple: true,
    form: {},
    rules: {
      createdTime: [{ required: true, message: "创建时间不能为空", trigger: "blur"     }],
      isDeleted: [{ required: true, message: "IsDeleted不能为空", trigger: "blur"     }],
    },
    options: {
      // 子篮类型 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
  typeOptions: [],
  statusOptions:[
  { dictLabel: '清洗线', dictValue: '1',langKey:"cleaningLineType.01" },
  { dictLabel: '缓存线', dictValue: '2',langKey:"cleaningLineType.02"}
  ]
    }
  })
  
  const { form, rules, options, single, multiple } = toRefs(state)
  
  proxy.getDicts('cks_part_type').then((response) => {
    state.options.typeOptions = response.data
})

  // 关闭dialog
  function cancel(){
    open.value = false
    reset()
  }
  
  // 重置表单
  function reset() {
    form.value = {
      id: null,
      blueCode: null,
      type: null,
      stock: null,
      updateTime: null,
      createdTime: null,
      isDeleted: null,
    };
    proxy.resetForm("formRef")
  }
  
  
  // 添加按钮操作
  function handleAdd() {
    reset();
    open.value = true
    title.value = '添加子篮信息'
    opertype.value = 1
  }
  // 修改按钮操作
  function handleUpdate(row) {
    reset()
    const id = row.id || ids.value
    getCksBasket(id).then((res) => {
      const { code, data } = res
      if (code == 200) {
        open.value = true
        title.value = '修改子篮信息'
        opertype.value = 2
  
        form.value = {
          ...data,
        }
      }
    })
  }
  
  // 添加&修改 表单提交
  function submitForm() {
    proxy.$refs["formRef"].validate((valid) => {
      if (valid) {
  
        if (form.value.id != undefined && opertype.value === 2) {
          updateCksBasket(form.value).then((res) => {
            proxy.$modal.msgSuccess("修改成功")
            open.value = false
            getList()
          })
        } else {
          addCksBasket(form.value).then((res) => {
              proxy.$modal.msgSuccess("新增成功")
              open.value = false
              getList()
            })
        }
      }
    })
  }
  
  // 删除按钮操作
  function handleDelete(row) {
    const Ids = row.id || ids.value
  
    proxy
      .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
        confirmButtonText: proxy.$t('common.ok'),
        cancelButtonText: proxy.$t('common.cancel'),
        type: "warning",
      })
      .then(function () {
        return delCksBasket(Ids)
      })
      .then(() => {
        getList()
        proxy.$modal.msgSuccess("删除成功")
      })
  }
  
  
  
  
  
  handleQuery()
/*** 导入参数 */
const upload = reactive({
  // 是否显示弹出层（用户导入）
  open: false,
  // 弹出层标题（用户导入）
  title: '',
  // 是否禁用上传
  isUploading: false,
  // 是否更新已经存在的用户数据
  updateSupport: 0,
  // 设置上传的请求头部
  headers: { Authorization: 'Bearer ' + getToken() },
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + '/business/CksBasket/importData'
})
/** 导入按钮操作 */
function handleImport() {
  upload.title = '导入子篮数据'
  upload.open = true
}
/** 下载模板操作 */
function importTemplate() {
  proxy.download('/business/CksBasket/downloadTemplate', '子篮数据模板')
}
/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
  upload.isUploading = true
}
/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
  const { code, msg, data } = response
  upload.open = false
  upload.isUploading = false
  proxy.$refs['uploadRef'].clearFiles()
  proxy.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +'已更新'+ '</div>', '导入结果', {
    dangerouslyUseHTMLString: true
  })
  getList()
}

/** 提交上传文件 */
function submitFileForm() {
  
  console.log('submitFileForm',proxy.$refs['uploadRef'])
  proxy.$refs['uploadRef'].submit()
}


  </script>